package br.com.gip.commons.base.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="PERFIL_ACESSO")
public class PerfilAcesso implements Serializable {

	private static final long serialVersionUID = -4807465437127330463L;

	@Id
	@Column(name="PERFIL_ACESSO_ID")
	@SequenceGenerator(name="SQ_PERFIL_ACESSO_PK", sequenceName="SQ_PERFIL_ACESSO_PK", allocationSize=1)
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SQ_PERFIL_ACESSO_PK")
	private Long perfilAcessoId;
	
	@Column(name="REGRA", unique=true, nullable=false, length=64)
	private String regra;

	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="USUARIO_PERFIL", 
		joinColumns = {
			@JoinColumn(name="ID_PERFIL_ACESSO")
		},
		inverseJoinColumns = {
			@JoinColumn(name="ID_USUARIO")
		}
	)
    private Set<Usuario> usuarios;
	
	public Long getPerfilAcessoId() {
		return perfilAcessoId;
	}

	public void setPerfilAcessoId(Long perfilAcessoId) {
		this.perfilAcessoId = perfilAcessoId;
	}

	public String getRegra() {
		return regra;
	}

	public void setRegra(String regra) {
		this.regra = regra;
	}

	public Set<Usuario> getUsuarios() {
		return usuarios;
	}

	public void setUsuarios(Set<Usuario> usuarios) {
		this.usuarios = usuarios;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((perfilAcessoId == null) ? 0 : perfilAcessoId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		PerfilAcesso other = (PerfilAcesso) obj;
		if (perfilAcessoId == null) {
			if (other.perfilAcessoId != null) {
				return false;
			}
		} else if (!perfilAcessoId.equals(other.perfilAcessoId)) {
			return false;
		}
		return true;
	}
	
}
